
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_boost/flutter_boost.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:yybb_flutter/Macros/yyb_macros.dart';
import 'package:yybb_flutter/http/api_response.dart';
import 'package:yybb_flutter/modules/abnormal_order/apiservise/order_api_servise.dart';
import 'package:yybb_flutter/modules/abnormal_order/models/yybb_abnomal_order_detail_model.dart';
import 'package:yybb_flutter/modules/abnormal_order/vmodel/abnormal_order_vmodel.dart';
import 'package:yybb_flutter/yybbtools/yybb_loading_Widget.dart';
import 'package:yybb_flutter/yybbtools/yybb_loading_nodata_widget.dart';
import 'package:yybb_flutter/yybbtools/yybb_services.dart';
import 'package:yybb_flutter/yybbtools/yybb_string_expands.dart';

import '../models/yybb_abnomal_order_detail_model.dart';

// ignore: must_be_immutable
class AbnormalOrderDetailPage extends StatefulWidget {
  AbnormalOrderDetailPage({Key key, this.params}) : super(key: key);
  Map<dynamic, dynamic> params;
  @override
  _AbnormalOrderDetailPageState createState() => new _AbnormalOrderDetailPageState();
}



class _AbnormalOrderDetailPageState extends State<AbnormalOrderDetailPage> {
  List _dataList = [];
  YYBBAbnormalOrderDetailModel _detailModel;
  @override
  void initState() {
    super.initState();
    YYBBPrintLog(widget.params["orderID"]);

    _getData(widget.params);
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading:
        IconButton(
          icon:
          ImageIcon(
            AssetImage("images/public/app_back_btn@3x.png"),
            size: 17,
            color: Colors.black,
          ),

          onPressed: (){
            FlutterBoost.singleton
                .close("abnormalOrderPage",
                exts: {"animated": true});
          },
        ),

        shadowColor:Colors.transparent,
        title:Text(
          "订单详情",
          style: TextStyle(
              color: Colors.black
          ),
        ),
        backgroundColor: Color.fromRGBO(250, 250, 250, 1),
      ),
      body: Container(
        child: _childWidgt(),
      ),
    );
  }


  Widget _childWidgt() {
    // return new ListView.builder(
    //   itemBuilder: _cellForRow,
    //   itemCount: _dataList.length > 0 ? _dataList.length : 0,
    // );
    Widget _childWidget;
    if (_dataList != null && _dataList.length != 0) {
      _childWidget = ListView.builder(
        itemBuilder: _cellForRow,
        itemCount: _dataList.length > 0 ? _dataList.length : 0,
      );
    } else {
      _childWidget = new YYBBNODataWidget();
      YYBBPlatform.invokeMapMethod("NodataCallBack");
    }
    return _childWidget;
  }


  // Cell 布局
  Widget _cellForRow(BuildContext context, int index ){
    return Container(
      alignment:Alignment.centerLeft ,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          _customerWidget(),
          Container(
            height: 10,
            color: YYBBColor245,
          ),
          _compeletProssWidget(),
          Container(
            height: 1,
            color: YYBBColor245,
          ),
          _goodsDetailWidget(),
          Container(
            height: 1,
            color: YYBBColor245,
          ),
          _goodsTotalWidget(),
          Container(
            height: 10,
            color: YYBBColor245,
          ),
           SizedBox(height: 15,),
          _orderInfoWidget(),
          SizedBox(height: 10,),
          Container(
            height: 10,
            color: YYBBColor245,
          ),
          _preferentialInfoWidget(),
          Container(
            height: 5,
            color: YYBBColor245,
          ),
          _totalPirceWidget(),

        ],
      ),
    );
  }

  // 客户信息
  Widget _customerWidget(){
    // print(a4.replaceFirstMapped("b", (Match match){//abcdea333 从index=2开始 用方法返回值替换指定的字符串
    //   return "333";
    // },2));
    YYBBAbnormalOrderModelLogistics item = _detailModel.logistics;
    final String address =  item.consigneeAddress.replaceAllMapped("^", (match) => "");

    return Container(
      padding: EdgeInsets.fromLTRB(12, 0, 12, 0),
      child: Column(
        children: [
          Container(
           child: Row(
            children: [
              Image(
                image: new AssetImage("images/public/customer_head@3x.png",),
                width: 15,
                height: 15,
              ),
              Text(
                " ${item.consignee} | ${item.consigneePhone}",
                textAlign: TextAlign.left,
                style: TextStyle(
                  fontSize: 14,
                  // fontWeight: FontWeight.w700,
                  color: YYBBColor51,
                ),
              ),

            ],
           ),
          ),
          SizedBox(height: 5,),
          Container(
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Image(
                  image: new AssetImage("images/public/customer_address@3x.png"),
                  width: 15,
                  height: 15,
                ),
                Expanded(
                  child: Text(
                    " $address",
                    textAlign: TextAlign.left,
                    // overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      fontSize: 14,
                      // fontWeight: FontWeight.w700,
                      color: YYBBColor51,
                    ),
                  ),
                ),
              ],
            ),
          ),
          SizedBox(height: 10,),
        ],
      ),
    );
  }
  // 完成进度
  Widget _compeletProssWidget(){
    String  imgs =  "images/abno_order/order_noselect@3x.png";
    if (_detailModel.orderProduct.orderProductStatus != null) {
      // 安全判断 后台返回字段太辣鸡
      if (_detailModel.orderProduct.orderProductStatus >5 ){
        _detailModel.orderProduct.orderProductStatus = 5;
      }
      if (_detailModel.orderProduct.orderProductStatus == 8){
        _detailModel.orderProduct.orderProductStatus = 0;
       }
      List orderImages = [
        "images/abno_order/order_noselect@3x.png",
        "images/abno_order/order_select_1@3x.png",
        "images/abno_order/order_select_2@3x.png",
        "images/abno_order/order_select_3@3x.png",
        "images/abno_order/order_select_4@3x.png",
        "images/abno_order/order_all_select@3x.png",
        "images/abno_order/order_all_select@3x.png"
      ];
      imgs = orderImages[_detailModel.orderProduct.orderProductStatus];
    }

    return Container(
      padding: EdgeInsets.fromLTRB(12, 0, 12, 0),
      child: Column(
        children: [
          SizedBox(height: 10,),
          Image(
            image: new AssetImage(imgs),
          ),
          SizedBox(height: 10,),
        ],
      ),
    );
  }
  // 商品信息
  Widget _goodsDetailWidget(){
    YYBBAbnormalOrderModelOrderProduct item = _detailModel.orderProduct;
    double transactionPrice = double.parse(item.transactionPrice);
    String  transactionPriceStr =  transactionPrice.toStringAsFixed(2);

    double pieceArea = double.parse(item.pieceArea);
    String  pieceAreaStr =  pieceArea.toStringAsFixed(2);

    double totalArea = double.parse(item.totalArea);
    String totalAreaStr =  totalArea.toStringAsFixed(2);

    String  lineSizeString =  AbnormalOrderVModel().getLinePressInfoWithSpecificationType(YYBBSpecificationType.carton, item);

    // String  lineSizeString = "";
    if( item.productRemark == null || item.productRemark.isEmpty ){
      item.productRemark = "暂无";
    }
    return Container (
      padding: EdgeInsets.fromLTRB(12, 0, 12, 0),
      child: Column(
        children: [
          SizedBox(height: 10,),
          Container(
            height: 72,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Container(
                  child: Row(
                    children:[
                      Image(
                        image: new NetworkImage(item.corrugatedTypeImg),
                        fit: BoxFit.cover,
                        width: 72,
                        height: 72,
                      ),
                      SizedBox(width: 12,),
                      Container(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              item.materialCode+"(${item.corrugatedType}楞)",
                              textAlign: TextAlign.left,
                              style: TextStyle(
                                fontSize: 16,
                                color: YYBBColor51,
                              ),
                            ),
                            SizedBox(height: 8,),
                            Text(
                          "￥$transactionPriceStr",
                              textAlign: TextAlign.left,
                              style: TextStyle(
                                fontSize: 14,
                                color: YYBBColorOrange,
                              ),
                            ),
                          ],
                        ),
                      ),
                    ],
                  ) ,
                ),

              ],
            ),
          ),
          SizedBox(height: 10,),
          Container(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  "长*宽:    ${item.sizeX}mm*${item.sizeY}mm",
                  textAlign: TextAlign.left ,
                  style: TextStyle(
                    fontSize: 14,
                    color: YYBBColor102,
                  ),
                ),
                Text(
                  "数量:${item.quantity}片",
                  textAlign: TextAlign.right,
                  style: TextStyle(
                    fontSize: 14,
                    color: YYBBColor102,
                  ),
                ),

              ],
            ),
          ),
          SizedBox(height: 8,),
          Container(
            alignment:Alignment.centerLeft ,
            child: Text(
              "压线信息:    $lineSizeString",
              textAlign: TextAlign.left ,
              style: TextStyle(
                fontSize: 14,
                color: YYBBColor102,
              ),
            ),
          ),
          SizedBox(height: 8,),
          Container(
            alignment:Alignment.centerLeft ,
            child: Text(
              "单片面积:    $pieceAreaStr㎡",
              textAlign: TextAlign.left ,
              style: TextStyle(
                fontSize: 14,
                color: YYBBColor102,
              ),
            ),
          ),
          SizedBox(height: 8,),
          Container(
            alignment:Alignment.centerLeft ,
            child: Text(
              "总面积:    $totalAreaStr㎡",
              textAlign: TextAlign.left ,
              style: TextStyle(
                fontSize: 14,
                color: YYBBColor102,
              ),
            ),
          ),
          SizedBox(height: 8,),
          Container(
            alignment:Alignment.centerLeft ,
            child: Text(
              "期望交货日:    ${item.expectedDeliveryTimeText}",
              textAlign: TextAlign.left ,
              style: TextStyle(
                fontSize: 14,
                color: YYBBColor102,
              ),
            ),
          ),
          SizedBox(height: 8,),
          Container(
            alignment:Alignment.centerLeft ,
            child: Text(
              "备注:     ${item.productRemark}",
              textAlign: TextAlign.left ,
              style: TextStyle(
                fontSize: 14,
                color: YYBBColor102,
              ),
            ),
          ),
          SizedBox(height: 10,),
        ],
      ),
    );
  }
  // 商品价格
  Widget _goodsTotalWidget(){
    YYBBAbnormalOrderModelOrderProduct item = _detailModel.orderProduct;
    double totalPrice = double.parse(item.productPrice);
    String  totalPriceStr =  totalPrice.toStringAsFixed(2);
    return Container (
      height: 50,
      // color: Colors.red,
      padding: EdgeInsets.fromLTRB(12, 0, 12, 0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          Text(
            "合计: ",
            textAlign: TextAlign.right ,
            style: TextStyle(
              fontSize: 14,
              color: YYBBColor102,
            ),
          ),
          Text(
            "￥$totalPriceStr",
            textAlign: TextAlign.right ,
            style: TextStyle(
              fontSize: 16,
              fontWeight: FontWeight.w700,
              color: YYBBColorRed,
            ),
          ),
        ],
      ),
    );
  }
  //订单信息
  Widget _orderInfoWidget(){
    List<String> logList = _detailModel.logList;
    return Container (
      padding: EdgeInsets.fromLTRB(12, 0, 12, 0),
      child: ListView.builder(
        itemBuilder:_orderInfoCellWidget ,
        itemCount: logList.length,
        physics:NeverScrollableScrollPhysics(),
        shrinkWrap:true,
      ),
    );
  }

  // 订单信息Cell
  Widget _orderInfoCellWidget(BuildContext context, int index ) {
    List<String> logList = _detailModel.logList;
    return Container(
      height: 25,
      child: Text(
        logList[index] ?? "",
        style: TextStyle (
          fontSize: 12,
          color: YYBBColor153,
        ),
      ),
    );
  }

  // 优惠金额
  Widget _preferentialInfoWidget(){
    double reducedAmount = double.parse(_detailModel.reducedAmount);
    String  reducedAmountStr =  reducedAmount.toStringAsFixed(2);
    double markupAmount = double.parse(_detailModel.markupAmount);
    String  markupAmountStr =  markupAmount.toStringAsFixed(2);
    return Container(
      padding: EdgeInsets.fromLTRB(12, 0, 12, 0),
      child: Column(
        children: [
          Container(
            height: 30,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  "减价金额:",
                  textAlign: TextAlign.left ,
                  style: TextStyle(
                    fontSize: 13,
                    color: YYBBColor102,
                  ),
                ),
                Text(
                  "-￥$reducedAmountStr",
                  textAlign: TextAlign.right,
                  style: TextStyle(
                    fontSize: 13,
                    color: YYBBColor153,
                  ),
                ),
              ],
            ),
          ),
          Container(
            height: 30,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  "加价金额:",
                  textAlign: TextAlign.left ,
                  style: TextStyle(
                    fontSize: 13,
                    color: YYBBColor102,
                  ),
                ),
                Text(
                  "+￥$markupAmountStr",
                  textAlign: TextAlign.right,
                  style: TextStyle(
                    fontSize: 13,
                    color: YYBBColor153,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  //总金额
  Widget _totalPirceWidget(){
    double totalPrice = double.parse(_detailModel.totalPrice);
    String  totalPriceStr =  totalPrice.toStringAsFixed(2);
    return Container(
      padding: EdgeInsets.fromLTRB(12, 0, 12, 0),
      height: 40,
      child:Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            "总金额:",
            textAlign: TextAlign.left ,
            style: TextStyle(
              fontSize: 13,
              color: YYBBColor102,
            ),
          ),
          Text(
            "￥$totalPriceStr",
            textAlign: TextAlign.right,
            style: TextStyle(
              fontSize: 16,
              fontWeight: FontWeight.w700,
              color: YYBBColorRed,
            ),
          ),
        ],
      ),
    );
  }

  // 获取数据
  void _getData(Map param) async {
    var params = Map<String, String>();
    params["orderID"] = param["orderID"].toString();
    // params["abnormalType"] = param["abnormalType"].toString();
    ApiResponse<YYBBAbnormalOrderDetailModel> item  = await AbnormalOrderVModel.getOrderDetailData(params);
    _detailModel = item.data;
    _dataList.clear();
    _dataList.add(_detailModel);
    // 刷新界面
    setState(() {});
  }

}











